package score;

import beans.SessionManagedBean;
import groups.ExerciseGroup;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureAdminManagedBean;
import lecture.LectureManagedBean;
import task.ExerciseSheet;
import user.User;
import user.UserAdminManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="tutorEnterScoreBean")
@CustomScoped(value="#{window}")
public class TutorEnterScoreManagedBean extends EnterScoreManagedBean {
    
    private List<ExerciseGroup> choosenGroups;
    private boolean set;
    
    public TutorEnterScoreManagedBean() {
        this.getGroups();
        this.choosenGroups = new ArrayList<ExerciseGroup>();
    }

    public List<ExerciseGroup> getChoosenGroups() {
        return choosenGroups;
    }

    public void setChoosenGroups(List<ExerciseGroup> choosenGroups) {
        //this.createCells(null, null)
        this.choosenGroups = choosenGroups;
    }
    
    public List<ExerciseGroup> getGroups() {
        FacesContext context = FacesContext.getCurrentInstance();        
        SessionManagedBean sb = (SessionManagedBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        LectureAdminManagedBean lectureAdmin = context.getApplication().evaluateExpressionGet(context, "#{lectureAdminBean}", LectureAdminManagedBean.class);
        User user = sb.getUser();
        Lecture lecture = sb.getLectureBean().getLecture();
        List<ExerciseGroup> list = new ArrayList<ExerciseGroup>();
        for (ExerciseGroup eg: lectureAdmin.getUserExerciseGroupsWhereLeader(user)) {
            if (eg.getLecture().getInfo().getName().equals(lecture.getInfo().getName()))
                list.add(eg);
                    
        }
        if (list.size() == 1) {
            this.set = true;
            this.choosenGroups = list;
        }
        return list;
    }
    
    public List<Map.Entry<String, List<Score>>> getScoreBySheet(ExerciseSheet sheet) {
        FacesContext context = FacesContext.getCurrentInstance();        
        SessionManagedBean sb = (SessionManagedBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        List<Map.Entry<String, List<Score>>> scores = new ArrayList<Map.Entry<String, List<Score>>>();
        List<String> students = new ArrayList<String>();
        for (ExerciseGroup eg: this.choosenGroups) {
            students.addAll(eg.getStudents());
        }
        
        for (Map.Entry<String, List<Score>> e: lecture.getScoreBySheet(sheet)) {
            if (students.contains(e.getKey())) {
                scores.add(e);
            }
        }
        return scores;
    }

    public boolean isSet() {
        return set;
    }

    public void setSet(boolean set) {
        this.set = set;
    }
    
    public List<ExerciseSheet> getSheets() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<ExerciseSheet> sheets = new ArrayList<ExerciseSheet>();
        Date d = null;
        Calendar calendar = new GregorianCalendar();
        
        for (ExerciseSheet es: lecture.getExerciseSheets()) {
            d = es.getDateOfReturn();
            calendar.setTime(d);
            calendar.add(Calendar.DAY_OF_MONTH, es.getCorrectionTime());
            if (d.after(new Date()) || (es.getCorrectionTime() == 0))
                sheets.add(es);
        }
        
        return sheets;
    }
    
    public void chooseGroups() {
        this.set = true;
        FacesContext c = FacesContext.getCurrentInstance();
        UserAdminManagedBean ua = c.getApplication().evaluateExpressionGet(c, "#{userAdminBean}", UserAdminManagedBean.class);
        List<User> user = new ArrayList<User>();
        for (ExerciseGroup e: this.choosenGroups) {
            for (String s: e.getStudents())
                user.add(ua.getUser(s));
        }
        this.userScores = createCells(this.choosenSheet, user);
    }
}
